package com.ponhu.ea2.goodsCenter.provider.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        String redisUrl = String.format("redis://%s:%s", redisProperties.getHost() + "", redisProperties.getPort() + "");
        config.useSingleServer().setAddress(redisUrl);
        if (StringUtils.isNotEmpty(redisProperties.getPassword())) {
            config.useSingleServer().setAddress(redisUrl).setPassword(redisProperties.getPassword());
        } else {
            config.useSingleServer().setAddress(redisUrl);
        }
        config.useSingleServer().setDatabase(4);
        return Redisson.create(config);
    }
}
